package com.partvoyage.client.mvp2.view.infopoint;

import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.HasClickHandlers;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;

public class InformationPointView extends SectionStack implements
		InformationPointPresenter.Display {
	// private SectionStackSection ouSectionStack;
	private SectionStackSection commentSectionStack;
	private SectionStackSection commentaireSectionStack;
	private SectionStackSection quandSectionStack;
	private SectionStackSection combienSectionStack;
	private SectionStackSection avecQuiSectionStack;

	/**
	 * Commentaire
	 */
	private final DynamicForm formCommentaire = new DynamicForm();
	private IButton validationCommentaire = new IButton();
	/**
	 * Quand
	 */
	private final DynamicForm formQuand = new DynamicForm();
	private IButton validationQuand = new IButton();
	private IButton synchroniserAvecCalendrier = new IButton();

	/**
	 * Avec Qui
	 */
	private IButton validationAvecQui = new IButton();
	private final ListGrid amisGrid = new ListGrid();
	/*
	 * Cout
	 */
	private final DynamicForm formCout = new DynamicForm();
	private TextItem coutField;
	private IButton validateCout = new IButton();
	/*
	 * Comment
	 */
	// private final DynamicForm formComment = new DynamicForm();
	private IButton validateComment = new IButton();

	private ListBox commentListBox;

	public InformationPointView() {
		super();
		setWidth100();
		// information point
		setVisibilityMode(VisibilityMode.MULTIPLE);
		setCanReorderSections(true);
		setCanResizeSections(false);
		setBorder("0px");
		setHeight100();
		// initialiserOu();
		initialiserComment();
		initialiserCout();
		initialiserAvecQui();
		initialiserQuand();
		initialiserCommentaire();
		// addSection(ouSectionStack);
		addSection(commentSectionStack);
		addSection(quandSectionStack);
		addSection(commentaireSectionStack);
		addSection(combienSectionStack);
		addSection(avecQuiSectionStack);

	}

	TextItem commentaireField;

	private void initialiserCommentaire() {
		commentaireSectionStack = new SectionStackSection();
		commentaireSectionStack.setTitle("Commentaire");
		commentaireSectionStack.setExpanded(false);
		commentaireSectionStack.setCanCollapse(true);

		// form.setDataSource(CommentaireDS.getInstance());
		formCommentaire.setUseAllDataSourceFields(true);
		formCommentaire.setWidth(300);
		// ajout du champs texte
		commentaireField = new TextItem("commentaire");
		formCommentaire.setFields(commentaireField);
		// ajout du bouton de validation

		validationCommentaire.setTitle("Ok");

		commentaireSectionStack.addItem(formCommentaire);
		commentaireSectionStack.addItem(validationCommentaire);

	}

	private void initialiserQuand() {
		quandSectionStack = new SectionStackSection();
		quandSectionStack.setTitle("Quand");
		quandSectionStack.setExpanded(false);
		quandSectionStack.setCanCollapse(true);

		formQuand.setWidth(400);
		formQuand.setDataSource(QuandDS.getInstance());
		validationQuand.setTitle("Ok");

		// bouton permettant de syncroniser la date avec le calendrier google

		synchroniserAvecCalendrier.setTitle("Sync avec GG Calendrier");

		quandSectionStack.addItem(formQuand);
		quandSectionStack.addItem(validationQuand);
		quandSectionStack.addItem(synchroniserAvecCalendrier);

	}

	/**
	 * ex.:
	 * http://www.topsourcecode.com/index.php/smartgwt-tutorials/73-smartgwt
	 * -dialogbox-widget-used-to-edit-listgrid-record-tutorial.html
	 */
	private void initialiserAvecQui() {
		avecQuiSectionStack = new SectionStackSection();
		avecQuiSectionStack.setTitle("Avec qui");
		avecQuiSectionStack.setExpanded(false);
		avecQuiSectionStack.setCanCollapse(true);

		amisGrid.setWidth100();
		amisGrid.setHeight("50%");
		amisGrid.setShowAllRecords(true);
		amisGrid.setCanEdit(true);
		amisGrid.setEditEvent(ListGridEditEvent.CLICK);
		amisGrid.setModalEditing(true);

		ListGridField nomAmiField = new ListGridField("nomAmi", "Ami");
		ListGridField viendraField = new ListGridField("viendra", "Viendra");
		viendraField.setType(ListGridFieldType.BOOLEAN);

		amisGrid.setFields(new ListGridField[] { nomAmiField, viendraField });

		// fin mock
		amisGrid.setData(getRecordsMock());

		validationAvecQui.setTitle("Ok");

		avecQuiSectionStack.addItem(amisGrid);
		avecQuiSectionStack.addItem(validationAvecQui);

	}

	public RecordAvecQui[] getRecordsMock() {
		return new RecordAvecQui[] { new RecordAvecQui("bob", Boolean.TRUE),
				new RecordAvecQui("bibi", Boolean.FALSE),
				new RecordAvecQui("toto", Boolean.TRUE),
				new RecordAvecQui("seb", Boolean.FALSE),
				new RecordAvecQui("nathalie", Boolean.FALSE) };
	}

	class RecordAvecQui extends ListGridRecord {
		public RecordAvecQui(String nomAmi, Boolean viendra) {

			setNomAmi(nomAmi);
			setViendra(viendra);
		}

		private void setNomAmi(String nomAmi) {
			setAttribute("nomAmi", nomAmi);
		}

		private void setViendra(Boolean viendra) {
			setAttribute("viendra", viendra);
		}
	}

	private void initialiserCout() {
		combienSectionStack = new SectionStackSection();
		combienSectionStack.setTitle("Combien");
		combienSectionStack.setExpanded(false);
		combienSectionStack.setCanCollapse(true);

		// DataSource dataSource = new DataSource();

		// DataSourceFloatField montantField = new
		// DataSourceFloatField("intField");
		// montantField.setTitle("Combien");

		// dataSource.setFields(montantField);
		// ajout du formulaire

		formCout.setWidth(300);
		// form.setDataSource(dataSource);
		// ajout du champs de texte
		coutField = new TextItem("cout");
		formCout.setFields(coutField);
		// ajout du bouton

		validateCout.setTitle("Ok");

		combienSectionStack.addItem(formCout);
		combienSectionStack.addItem(validateCout);
	}

	public void miseAJourCout(String cout) {
		coutField.setValue(cout);

	}

	private Label couLabel = new Label();
	private Label ouLabel = new Label();

	// private void initialiserOu() {
	// ouSectionStack = new SectionStackSection();
	// ouSectionStack.setTitle("Ou");
	// ouSectionStack.setExpanded(true);
	// ouSectionStack.setCanCollapse(true);
	// ouSectionStack.addItem(ouLabel);
	// }

	public void miseAJourOu(String ou) {
		ouLabel.setContents(ou);
	}

	private void initialiserComment() {
		commentSectionStack = new SectionStackSection();
		commentSectionStack.setTitle("Comment");
		commentSectionStack.setExpanded(true);
		commentSectionStack.setCanCollapse(true);

		commentListBox = new ListBox();
		validateComment = new IButton("ok");

		HLayout hLayout = new HLayout();
		hLayout.addMember(commentListBox);
		hLayout.addMember(validateComment);
		commentSectionStack.addItem(hLayout);

	}

	public ListBox getCommentSelectItem() {
		return commentListBox;
	}

	public void initialiseComment(List<String> commentList) {
		int i = 0;
		commentListBox.clear();
		for (String comment : commentList) {
			commentListBox.addItem(comment);
		}
	}

	public Integer getCommentSelectionne() {
		return commentListBox.getSelectedIndex();
	}

	public Widget asWidget() {
		return this;
	}

	public HasClickHandlers getValidationCommentaire() {
		return validationCommentaire;
	}

	public HasClickHandlers getValidationQuand() {
		return validationQuand;
	}

	public void miseAJourCommentaire(String commentaire) {
		commentaireField.setValue(commentaire);
	}

	public void miseAJourQuand(Date quand) {
		formQuand.getField(QuandDS.ID).setValue(quand);
	}

	//
	// public SectionStackSection getOuSectionStack() {
	// return ouSectionStack;
	// }

	public SectionStackSection getCommentSectionStack() {
		return commentSectionStack;
	}

	public SectionStackSection getCommentaireSectionStack() {
		return commentaireSectionStack;
	}

	public SectionStackSection getQuandSectionStack() {
		return quandSectionStack;
	}

	public SectionStackSection getCombienSectionStack() {
		return combienSectionStack;
	}

	public SectionStackSection getAvecQuiSectionStack() {
		return avecQuiSectionStack;
	}

	public DynamicForm getFormCommentaire() {
		return formCommentaire;
	}

	public DynamicForm getFormQuand() {
		return formQuand;
	}

	public IButton getSynchroniserAvecCalendrier() {
		return synchroniserAvecCalendrier;
	}

	public IButton getValidationAvecQui() {
		return validationAvecQui;
	}

	public ListGrid getAmisGrid() {
		return amisGrid;
	}

	public DynamicForm getFormCout() {
		return formCout;
	}

	public IButton getValidateCout() {
		return validateCout;
	}

	//
	// public DynamicForm getFormComment() {
	// return formComment;
	// }

	public IButton getValidateComment() {
		return validateComment;
	}

	public TextItem getCommentaireField() {
		return commentaireField;
	}

	public TextItem getCoutField() {
		return coutField;
	}

	public Label getCouLabel() {
		return couLabel;
	}

}
